package admin

import (
	"github.com/gin-gonic/gin"
	"jwgin/model/mysql"
	"jwgin/service"
)

type Activity struct{}

func (l *Activity) Routers(Router *gin.RouterGroup) {
	Router.GET("activity", l.get)
	Router.GET("activity/com", l.getCom)
	Router.GET("activity/temp", l.getTemp)
	Router.PUT("activity", l.put)
	Router.POST("activity", l.post)
}

//前置操作
func (Activity) getCom(c *gin.Context) {
	resp := &mysql.Company{
		State: 1,
	}
	RespSucc(c, resp.GetAll())
}
func (Activity) getTemp(c *gin.Context) {
	resp := &mysql.ActivityTemplate{
		TemplateStatu: 1,
	}
	RespSucc(c, resp.GetAll())
}

//活动列表
func (Activity) get(c *gin.Context) {
	count, size, offset := pages(c)
	db := service.G.Model(mysql.Activity{}).Joins("JOIN `company` ON company.id = activity.comid").Joins("JOIN `activity_template` ON activity_template.id = activity.activity_template_id")
	//时间筛选
	startTime := c.Query("startTime")
	endTime := c.Query("endTime")
	if startTime != "" && endTime != "" {
		db = db.Where(" user.addtime > ? AND user.addtime < ? ", startTime, endTime)
	}

	list := []map[string]interface{}{}
	db.Select("title,template_name,activity.*").Count(&count).Offset(offset).Limit(size).Order("id DESC").Scan(&list)
	RespList(c, count, list)
}

//添加
func (Activity) put(c *gin.Context) {
	requ := &mysql.Activity{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}
	service.G.Create(requ)
	RespSucc(c, nil)
}

//更新
func (Activity) post(c *gin.Context) {
	requ := &mysql.Activity{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}
	service.G.Where("id = ?", requ.Id).Updates(requ)
	RespSucc(c, nil)
}
